
package com.ami.vmedia.gui;

import com.ami.iusb.IUSBRedirSession;
import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.vmedia.VMApp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.io.File;
import java.io.PrintStream;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class DevicePanel extends JPanel {
	public static final String PHYSICAL_DRIVE_WIN = LocaleStrings.getString("A_5_DP");
	public static final String LOGICAL_DRIVE_WIN = LocaleStrings.getString("A_6_DP");
	public static final String HD_DRIVE_LIN = "/dev/sd";
	private static final long serialVersionUID = 1L;
	private final Color DEV_PANEL_BG_COLOR = Color.WHITE;

	private String[] imagePathList;

	private ButtonGroup deviceSelectionGroup;

	private JRadioButton[] selectDevice;

	private int totalDrives;

	private JPanel panelImage;

	private String imageSize;

	private String imagePath;

	private long freeDiskSize;

	private int deviceType;
	public JButton folderBrowseBtn;
	public JTextField folderTxt;
	public boolean hd_flag = false;

	public JButton browseButton;

	public JComboBox imageAddressBar;

	public String selectedImagePath;

	public double getDirSize(File paramFile) {
		if (paramFile.exists()) {
			if (paramFile.isDirectory()) {
				File[] arrayOfFile1 = paramFile.listFiles();
				double d2 = 0.0D;
				for (File localFile : arrayOfFile1)
					d2 += getDirSize(localFile);
				return d2;
			}
			double d1 = paramFile.length() / 1024.0D / 1024.0D;
			return d1;
		}

		System.out.println("文件或者文件夹不存在，请检查路径是否正确！");
		return 0.0D;
	}

	public DevicePanel(String[] paramArrayOfString, int paramInt1, int paramInt2) {
		super(new GridLayout(
				(paramArrayOfString != null) && (paramArrayOfString.length > 1) ? paramArrayOfString.length + 1 : 2, 1,
				0, 0));

		this.deviceType = paramInt1;
		int i = 1;

		this.deviceSelectionGroup = new ButtonGroup();
		addDeviceSelectors(paramInt1, paramArrayOfString);

		this.panelImage = new JPanel(new BorderLayout(5, 5));
		this.panelImage.setBackground(this.DEV_PANEL_BG_COLOR);

		createImageAddressBar(paramInt1, paramInt2);

		JPanel localJPanel = new JPanel(new GridBagLayout());
		localJPanel.setBackground(this.DEV_PANEL_BG_COLOR);
		GridBagConstraints localGridBagConstraints1 = new GridBagConstraints();
		localGridBagConstraints1.insets = new Insets(0, 2, 0, 5);
		this.browseButton = createBrowseButton(paramInt1, paramInt2);
		localJPanel.add(this.browseButton, localGridBagConstraints1);

		this.panelImage.add(this.selectDevice[0], "West");
		this.panelImage.add(this.imageAddressBar, "Center");
		this.panelImage.add(localJPanel, "East");

		setLayout(new GridBagLayout());
		GridBagConstraints localGridBagConstraints2 = new GridBagConstraints();

		localGridBagConstraints2.gridx = 0;
		localGridBagConstraints2.gridy = 0;
		localGridBagConstraints2.gridwidth = 1;
		localGridBagConstraints2.gridheight = 1;
		localGridBagConstraints2.weightx = 1.0D;
		localGridBagConstraints2.anchor = 17;
		localGridBagConstraints2.fill = 2;

		add(this.panelImage, localGridBagConstraints2);

		if (paramInt1 == 2) {

			setFolderImagePath(System.getProperty("user.home"));
			this.panelImage = new JPanel(new BorderLayout(5, 5));
			this.panelImage.setBackground(this.DEV_PANEL_BG_COLOR);

			localJPanel = new JPanel(new GridBagLayout());
			localJPanel.setBackground(this.DEV_PANEL_BG_COLOR);

			localGridBagConstraints1.insets = new Insets(0, 2, 0, 5);
			this.folderBrowseBtn = getFolderBrowseBtn(paramInt2);
			localJPanel.add(this.folderBrowseBtn, localGridBagConstraints1);

			this.panelImage.add(this.selectDevice[1], "West");
			this.panelImage.add(this.folderTxt, "Center");
			this.panelImage.add(localJPanel, "East");

			localGridBagConstraints2.insets = new Insets(5, 0, 0, 0);
			localGridBagConstraints2.gridy = 1;
			add(this.panelImage, localGridBagConstraints2);

			this.folderBrowseBtn.setEnabled(false);
			this.folderTxt.setVisible(true);

			this.folderTxt.setEnabled(true);

			i = 2;
		}
		if (paramInt1 == 2) {
			localGridBagConstraints2.insets = new Insets(10, 0, 0, 0);
		} else {
			localGridBagConstraints2.insets = new Insets(5, 0, 0, 0);
		}

		localGridBagConstraints2.insets = new Insets(0, 0, 0, 0);
		for (; i < this.totalDrives; i++) {
			localGridBagConstraints2.gridy = (i + 10);
			add(this.selectDevice[i], localGridBagConstraints2);
		}
		if (paramInt1 == 2) {

			if (System.getProperty("os.name").toLowerCase().contains("windows")) {
				enableHardDiskDrives(PHYSICAL_DRIVE_WIN);
			} else {
				enableHardDiskDrives("/dev/sd");
			}
		}

		setBackground(this.DEV_PANEL_BG_COLOR);
	}

	public void setFreeDiskSize(long paramLong) {
		this.freeDiskSize = paramLong;
	}

	public long getFreeDiskSize() {
		return this.freeDiskSize;
	}

	public void setFolderImageSize(String paramString) {
		this.imageSize = paramString;
	}

	public String getFolderImageSize() {
		return this.imageSize;
	}

	public void setFolderImagePath(String paramString) {
		this.imagePath = paramString;
	}

	public String getFolderImagePath() {
		return this.imagePath;
	}

	private JButton getFolderBrowseBtn(int paramInt) {
		if (this.folderBrowseBtn == null) {
			this.folderBrowseBtn = new JButton();
			this.folderBrowseBtn.setActionCommand("FOLDER_BROWSE_HD_" + paramInt);
			this.folderBrowseBtn.setText(LocaleStrings.getString("A_1_DP"));
			this.folderBrowseBtn.addActionListener(new VMActionListener());
		}
		return this.folderBrowseBtn;
	}

	class RadioButtonChangeListener implements ChangeListener {
		RadioButtonChangeListener() {
		}

		public void stateChanged(ChangeEvent paramChangeEvent) {
			if (DevicePanel.this.hd_flag) {

				if (DevicePanel.this.selectDevice[0].isSelected()) {
					if (DevicePanel.this.folderBrowseBtn != null) {
						DevicePanel.this.folderBrowseBtn.setEnabled(false);
					}

					DevicePanel.this.folderTxt.setEnabled(false);

					if (DevicePanel.this.browseButton != null)
						DevicePanel.this.browseButton.setEnabled(true);
					if (DevicePanel.this.imageAddressBar != null) {
						DevicePanel.this.imageAddressBar.setEnabled(true);
					}
				} else if (DevicePanel.this.selectDevice[1].isSelected()) {
					if (DevicePanel.this.folderBrowseBtn != null) {
						DevicePanel.this.folderBrowseBtn.setEnabled(true);
					}

					DevicePanel.this.folderTxt.setEnabled(true);

				} else {

					DevicePanel.this.browseButton.setEnabled(false);
					DevicePanel.this.folderBrowseBtn.setEnabled(false);

					DevicePanel.this.folderTxt.setEnabled(false);

					if (DevicePanel.this.imageAddressBar != null) {
						DevicePanel.this.imageAddressBar.setEnabled(false);
					}

				}

			} else if (DevicePanel.this.selectDevice[0].isSelected()) {
				if (DevicePanel.this.browseButton != null)
					DevicePanel.this.browseButton.setEnabled(true);
				if (DevicePanel.this.imageAddressBar != null) {
					DevicePanel.this.imageAddressBar.setEnabled(true);
				}
			} else {
				DevicePanel.this.browseButton.setEnabled(false);
				if (DevicePanel.this.imageAddressBar != null) {
					DevicePanel.this.imageAddressBar.setEnabled(false);
				}
			}
		}
	}

	public boolean isImageSelected() {
		if (this.selectDevice[0].getModel() == this.deviceSelectionGroup.getSelection())
			return true;
		return false;
	}

	public boolean isFolderMountSelected() {
		if (this.deviceType == 2) {
			if (this.selectDevice[1].getModel() == this.deviceSelectionGroup.getSelection())
				return true;
		}
		return false;
	}

	public void setImagePath(String paramString, int paramInt1, int paramInt2) {
		int i = 0;

		String[] arrayOfString = null;
		switch (paramInt1) {
		case 0:
			arrayOfString = VMApp.cdImagePath[paramInt2];
			break;
		case 1:
			arrayOfString = VMApp.fdImagePath[paramInt2];
			break;
		case 2:
			arrayOfString = VMApp.hdImagePath[paramInt2];
		}

		this.imagePathList = new String[5];
		if (this.imageAddressBar.getItemAt(0) == null) {
			this.imageAddressBar.insertItemAt(paramString, 0);
			this.imagePathList[0] = paramString;

		} else {
			for (i = 4; i >= 0; i--) {
				if ((this.imageAddressBar.getItemAt(i) != null)
						&& (this.imageAddressBar.getItemAt(i).equals(paramString))) {
					this.imageAddressBar.removeItemAt(i);
					break;
				}
			}

			if ((this.imageAddressBar.getItemAt(5) != null)
					&& (!paramString.equals(this.imageAddressBar.getItemAt(5)))) {
				this.imageAddressBar.removeItemAt(5);
			}

			if (!paramString.equals(this.imageAddressBar.getItemAt(0))) {
				this.imageAddressBar.insertItemAt(paramString, 0);
				for (i = 0; i < 5; i++) {
					if ((this.imageAddressBar.getItemAt(1) != null)
							&& (this.imageAddressBar.getItemAt(1).equals(arrayOfString[i])))
						break;
				}
				if (i == 5) {
					this.imageAddressBar.removeItemAt(1);
				}
			}

			for (i = 0; i < 5; i++) {
				if ((String) this.imageAddressBar.getItemAt(i) == null)
					break;
				this.imagePathList[i] = ((String) this.imageAddressBar.getItemAt(i));
			}
		}

		this.imageAddressBar.setSelectedIndex(0);
	}

	public void saveImagePath(String[] paramArrayOfString, int paramInt1, int paramInt2) {
		if (this.imageAddressBar.getItemAt(5) != null) {
			this.imageAddressBar.removeItemAt(5);
		}
		switch (paramInt1) {
		case 0:
			VMApp.cdImagePath[paramInt2] = paramArrayOfString;
			break;
		case 1:
			VMApp.fdImagePath[paramInt2] = paramArrayOfString;
			break;
		case 2:
			VMApp.hdImagePath[paramInt2] = paramArrayOfString;
		}

	}

	public String getImagePath() {
		String str = (String) this.imageAddressBar.getSelectedItem();

		if (str == null) {
			str = "";
		}
		return str;
	}

	public String getSelectedDeviceString() {
		for (Enumeration localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration
				.hasMoreElements();) {
			JRadioButton localJRadioButton = (JRadioButton) localEnumeration.nextElement();
			if (localJRadioButton.getModel() == this.deviceSelectionGroup.getSelection())
				return localJRadioButton.getText();
		}
		return "";
	}

	public void disableAll() {
		for (int i = 0; i < this.selectDevice.length; i++) {
			this.selectDevice[i].setEnabled(false);
		}

		this.browseButton.setEnabled(false);

		this.imageAddressBar.setEnabled(false);
		if (this.hd_flag) {
			this.folderBrowseBtn.setEnabled(false);

			this.folderTxt.setEnabled(false);
		}
	}

	public void enableAll() {
		for (int i = 0; i < this.selectDevice.length; i++) {
			if (!isDeviceredirected(this.selectDevice[i].getText())) {
				this.selectDevice[i].setEnabled(true);
			} else {
				this.selectDevice[i].setEnabled(false);
			}
		}

		if (this.selectDevice[0].isSelected()) {
			this.browseButton.setEnabled(true);

			this.imageAddressBar.setEnabled(true);
		}
		if ((this.hd_flag) && (this.selectDevice[1].isSelected())) {
			this.folderBrowseBtn.setEnabled(true);

			this.folderTxt.setEnabled(true);
		}
	}

	public boolean isDeviceredirected(String paramString) {
		for (int i = 0; i < this.selectDevice.length; i++) {
			if (VMApp.getInstance().getIUSBRedirSession() != null) {
				VMApp.getInstance().getIUSBRedirSession();
				if ((VMApp.getInstance().getIUSBRedirSession().getDeviceRedirStatus(this.deviceType, i) == 1)
						&& (paramString.equals(
								VMApp.getInstance().getIUSBRedirSession().getDeviceRedirSource(this.deviceType, i)))) {
					return true;
				}
			}
		}

		return false;
	}

	public void selectRadioButton(String paramString) {
		for (Enumeration localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration
				.hasMoreElements();) {
			JRadioButton localJRadioButton;
			if (this.hd_flag) {
				localJRadioButton = (JRadioButton) localEnumeration.nextElement();
				if (localJRadioButton.isSelected()) {
					localJRadioButton.setSelected(true);
					return;
				}
			} else {
				localJRadioButton = (JRadioButton) localEnumeration.nextElement();
				if (localJRadioButton.getText().equals(paramString)) {
					localJRadioButton.setSelected(true);
					return;
				}
			}
		}

		JRadioButton localJRadioButton;

		Enumeration<AbstractButton> localEnumeration = this.deviceSelectionGroup.getElements();
		if (localEnumeration.hasMoreElements()) {
			localJRadioButton = (JRadioButton) localEnumeration.nextElement();
			localJRadioButton.setSelected(true);
		}
	}

	public String[] getImagePathList() {
		int i = this.imageAddressBar.getSelectedIndex();
		String str = (String) this.imageAddressBar.getSelectedItem();

		if (i != 0)
			for (int j = i; j > 0; j--)
				this.imagePathList[j] = this.imagePathList[(j - 1)];
		this.imagePathList[0] = str;
		return this.imagePathList;
	}

	public void setImagePathList(String[] paramArrayOfString) {
		this.imagePathList = paramArrayOfString;
	}

	public String getSelectedImagePath() {
		return this.selectedImagePath;
	}

	public void setSelectedImagePath(String paramString) {
		this.selectedImagePath = paramString;
	}

	private JButton createBrowseButton(int paramInt1, int paramInt2) {
		JButton localJButton = new JButton(LocaleStrings.getString("A_1_DP"));
		switch (paramInt1) {
		case 0:
			localJButton.setActionCommand("BROWSE_CD_" + paramInt2);
			break;
		case 1:
			localJButton.setActionCommand("BROWSE_FD_" + paramInt2);
			break;
		case 2:
			localJButton.setActionCommand("BROWSE_HD_" + paramInt2);
			break;
		}

		localJButton.addActionListener(new VMActionListener());
		return localJButton;
	}

	private JComboBox createImageAddressBar(int paramInt1, int paramInt2) {
		switch (paramInt1) {
		case 0:
			this.imageAddressBar = new JComboBox(VMApp.cdImagePath[paramInt2]);
			this.imageAddressBar.setActionCommand("CD_IMAGE_PATH_ADDRESS_" + paramInt2);
			this.imagePathList = VMApp.cdImagePath[paramInt2];
			break;
		case 1:
			this.imageAddressBar = new JComboBox(VMApp.fdImagePath[paramInt2]);
			this.imageAddressBar.setActionCommand("FD_IMAGE_PATH_ADDRESS_" + paramInt2);
			this.imagePathList = VMApp.fdImagePath[paramInt2];
			break;
		case 2:
			this.imageAddressBar = new JComboBox(VMApp.hdImagePath[paramInt2]);
			this.imageAddressBar.setActionCommand("HD_IMAGE_PATH_ADDRESS_" + paramInt2);
			this.imagePathList = VMApp.hdImagePath[paramInt2];
		}

		this.imageAddressBar.setEditable(false);
		this.imageAddressBar.setBackground(this.DEV_PANEL_BG_COLOR);
		this.imageAddressBar.setBorder(null);
		this.imageAddressBar.setMaximumRowCount(5);
		this.imageAddressBar.addActionListener(new VMActionListener());

		return this.imageAddressBar;
	}

	public void addDeviceSelectors(int paramInt, String[] paramArrayOfString) {
		int i = 1;
		if (paramInt == 2) {
			this.totalDrives = (paramArrayOfString != null ? paramArrayOfString.length + 2 : 2);
		} else {
			this.totalDrives = (paramArrayOfString != null ? paramArrayOfString.length + 1 : 1);
		}
		this.selectDevice = new JRadioButton[this.totalDrives];
		switch (paramInt) {
		case 0:
			this.selectDevice[0] = new JRadioButton(LocaleStrings.getString("A_2_DP"));
			break;
		case 1:
			this.selectDevice[0] = new JRadioButton(LocaleStrings.getString("A_3_DP"));
			break;
		case 2:
			this.selectDevice[0] = new JRadioButton(LocaleStrings.getString("A_4_DP"));
			this.selectDevice[1] = new JRadioButton("Folder Path");

			this.folderTxt = new JTextField(20);

			this.folderTxt.setEditable(false);
			this.folderTxt.setBackground(this.DEV_PANEL_BG_COLOR);

			i = 2;
			this.hd_flag = true;
		}

		this.selectDevice[0].setBackground(this.DEV_PANEL_BG_COLOR);
		this.selectDevice[0].addChangeListener(new RadioButtonChangeListener());
		this.deviceSelectionGroup.add(this.selectDevice[0]);
		this.selectDevice[0].setSelected(true);
		if (paramInt == 2) {
			this.selectDevice[1].setBackground(this.DEV_PANEL_BG_COLOR);
			this.selectDevice[1].addChangeListener(new RadioButtonChangeListener());
			this.deviceSelectionGroup.add(this.selectDevice[1]);
		}

		int j = i;
		for (int k = 0; j < this.totalDrives; k++) {
			this.selectDevice[j] = new JRadioButton(paramArrayOfString[k]);
			this.selectDevice[j].setBackground(this.DEV_PANEL_BG_COLOR);
			this.deviceSelectionGroup.add(this.selectDevice[j]);
			j++;
		}
	}

	public void removeDeviceSelectors() {
		for (int i = 0; i < this.selectDevice.length; i++) {
			this.deviceSelectionGroup.remove(this.selectDevice[i]);
			remove(this.selectDevice[i]);
		}
	}

	public void enableHardDiskDrives(String paramString) {
		for (int i = 2; i < this.selectDevice.length; i++) {
			if (this.selectDevice[i].getText().startsWith(paramString)) {
				this.selectDevice[i].setVisible(true);
			} else {
				this.selectDevice[i].setVisible(false);
			}
		}
	}

	private void changeStateLogicalDrives(String paramString, boolean paramBoolean) {
		String[] arrayOfString1 = null;
		try {
			arrayOfString1 = paramString.substring(paramString.indexOf('[') + 1, paramString.indexOf(']')).split("-");
		} catch (Exception localException1) {
			Debug.out.println(localException1);
			arrayOfString1 = null;
			return;
		}

		for (Enumeration localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration
				.hasMoreElements();) {
			JRadioButton localJRadioButton = (JRadioButton) localEnumeration.nextElement();

			if (localJRadioButton.getText().startsWith(LOGICAL_DRIVE_WIN)) {
				String str1 = null;
				try {
					str1 = localJRadioButton.getText().substring(localJRadioButton.getText().indexOf('[') + 1,
							localJRadioButton.getText().indexOf(']'));
				} catch (Exception localException2) {
					Debug.out.println(localException2);
					str1 = null;
					return;
				}

				for (String str2 : arrayOfString1) {
					if ((str1.equals(str2)) && (localJRadioButton.isEnabled() != paramBoolean)) {
						localJRadioButton.setEnabled(paramBoolean);
						break;
					}
				}
			}
		}
	}

	private void changeStatePhysicalDrives(String paramString, boolean paramBoolean) {
		for (Enumeration localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration
				.hasMoreElements();) {
			JRadioButton localJRadioButton = (JRadioButton) localEnumeration.nextElement();

			if (localJRadioButton.getText().startsWith(PHYSICAL_DRIVE_WIN)) {

				if ((paramString.equalsIgnoreCase(localJRadioButton.getText()))
						&& (localJRadioButton.isEnabled() != paramBoolean)) {
					localJRadioButton.setEnabled(paramBoolean);
					break;
				}
			}
		}
	}

	public void updatePhysicalDeviceState(int paramInt, String paramString, boolean paramBoolean) {
		Enumeration localEnumeration;

		if (paramInt == 2) {
			if (System.getProperty("os.name").toLowerCase().contains("windows")) {
				changeStatePhysicalDrives(paramString, paramBoolean);
				changeStateLogicalDrives(paramString, paramBoolean);
			} else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
				changeStateHDDrives(paramString, paramBoolean);
			}
		} else {
			for (localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration.hasMoreElements();) {
				JRadioButton localJRadioButton = (JRadioButton) localEnumeration.nextElement();
				if (localJRadioButton.getText().equals(paramString)) {
					localJRadioButton.setEnabled(paramBoolean);
				}
			}
		}
	}

	private void changeStateHDDrives(String paramString, boolean paramBoolean) {
		int i;
		try {
			i = paramString.charAt("/dev/sd".length());
		} catch (Exception localException1) {
			Debug.out.println(localException1);
			return;
		}

		for (Enumeration localEnumeration = this.deviceSelectionGroup.getElements(); localEnumeration
				.hasMoreElements();) {
			JRadioButton localJRadioButton = (JRadioButton) localEnumeration.nextElement();
			int j;
			try {
				j = localJRadioButton.getText().charAt("/dev/sd".length());
			} catch (Exception localException2) {
				Debug.out.println(localException2);
				return;
			}

			if ((i == j) && (localJRadioButton.isEnabled() != paramBoolean)) {
				localJRadioButton.setEnabled(paramBoolean);
				break;
			}
		}
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\vmedia\gui\DevicePanel.class
 * Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */